/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.ctrl;

import com.cxhd.lis.model.newmodel.LisSampletype;
import com.cxhd.lis.model.newmodel.LisTestdescribe;
import com.cxhd.lis.model.newmodel.PpDeviceTest;
import static com.cxhd.lis.model.newmodel.PpDeviceTest.ppDeviceTestdao;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import java.util.List;
import java.util.Map;

/**
 *
 * @author cxhd
 */
@ControllerBind(controllerKey = "/view/lis/pp")
public class PpDeviceTestCtil extends Controller {

    Gson gson = new Gson();
    private LisTestdescribe test = new LisTestdescribe();
    private LisSampletype sam = new LisSampletype();

    public void index() {
        render("/view/lis/yqxm.jsp");
    }

    /**
     * 仪器显示
     */
    public void getLisDevices() {
        renderJson(Db.use("mysql").find("select s.PK DEVICEPK,s.DEVICEMODEL,s.DEVICEID,s.DEVICENAME ,\n"
                + "(SELECT DEVICETYPEDESC FROM lis_devicetype d WHERE d.DEVICETYPE=s.DEVICETYPE ) DEVICETYPE1 \n"
                + " from lis_deviceset s"));
    }

    /**
     * 项目显示
     */
    public void getLisTest() {
//       renderJson( test.getLisTests(getPara("PK")));
        String sql = "select t.PK TESTPK,t.PRICE, t.ENGLISHNAME,t.CHINESENAME,t.TESTID ,t.SAMPLETYPE,(SELECT s.SAMPLEDESCRIBE  FROM lis_sampletype s WHERE s.PK=t.SAMPLETYPE) SAMPLEDESCRIBE ";
        String param = "from lis_testdescribe t WHERE  t.SAMPLETYPE=" + Integer.valueOf(getPara("PK"));
        Page<Record> userPage = Db.use("mysql").paginate(getParaToInt("currentPage", getParaToInt("currentPage")), getParaToInt("pageSize", getParaToInt("pageSize")), sql, param);
        renderJson(userPage);
    }

    /**
     * 获取标本类型 ---用于筛选项目
     */
    public void getLisSam() {
        renderJson(sam.getLisSams());
    }

    /**
     * 显示选中仪器的匹配
     */
    public void getPpDeviceTest() {
//        renderJson(Db.use("mysql").find("select (SELECT t.CHINESENAME FROM lis_testdescribe t WHERE p.TESTID=t.TESTID ) CHINESENAME,\n" +
//"(SELECT  d.DEVICENAME FROM lis_deviceset d WHERE d.DEVICEID=p.DEVICEID) DEVICENAME,\n" +
//"p.TESTID,p.DEVICEID,p.DATA1,p.DATA2,p.PK from pp_device_test p where p.DEVICEID=?",getPara("DEVICEID")));
        String sql = "select (SELECT t.CHINESENAME FROM lis_testdescribe t WHERE p.TESTID=t.TESTID ) CHINESENAME,(SELECT  d.DEVICENAME FROM lis_deviceset d WHERE d.DEVICEID=p.DEVICEID) DEVICENAME,p.TESTID,p.DEVICEID,p.DATA1,p.DATA2,p.PK,p.UNIT,p.MREFLO0,p.MREFHI0,p.FREFLO0,p.FREFHI0,p.Decimalplace,p.SAMPLETYPE ";
        String param = "from pp_device_test p where p.DEVICEID='" + getPara("DEVICEID") + "' order by DATA1";
        System.out.println(sql + " " + param);
        Page<Record> userPage = Db.use("mysql").paginate(getParaToInt("currentPage", getParaToInt("currentPage")), getParaToInt("pageSize", getParaToInt("pageSize")), sql, param);
        renderJson(userPage);
    }

    /**
     * 待完成 保存仪器与项目的匹配
     */
    @Before(Tx.class)
    public void savePpDeviceTest() {
//        Map<String, String> test = gson.fromJson(getPara("TESTS"), new TypeToken<Map<String, String>>() {}.getType());
        List<Map<String, String>> tests = gson.fromJson(getPara("TESTS"), new TypeToken<List<Map<String, String>>>() {
        }.getType());
        //查询该仪器是否匹配相关样品类型的项目
//        List<Record> list=Db.use("mysql").find("select * from pp_device_test where SAMPLETYPE=? and DEVICEID=? limit 0,10",tests.get(0).get("SAMPLETYPE"),getPara("DEVICEID"));
//        //删除改仪器相关样品类型的所有项目匹配
//        if(list.size()>0){
////        Db.use("mysql").update("delete from pp_device_test where SAMPLETYPE=?",tests.get(0).get("SAMPLETYPE"));
//        ppDeviceTestdao.delectPpDeviceTests(tests.get(0).get("SAMPLETYPE"), getPara("DEVICEID"));
//        }

        //为该仪器添加项目
        for (Map<String, String> tmp : tests) {
            List<Record> list = Db.use("mysql").find("select * from pp_device_test where DEVICEID=? AND TESTID=?", getPara("DEVICEID"), tmp.get("TESTID"));
            if (list.size() == 0) {
                new PpDeviceTest().savePpDeviceTest(getPara("DEVICEID"), tmp.get("TESTID"), getData1(getPara("DEVICEID")), tmp.get("TESTPK"), getPara("DEVICEPK"));
                
            }
        }
        renderText("true");
    }

    /**
     * 注意删除需要一些必要的判断 该匹配是否被使用 待完成 删除仪器与项目的匹配
     */
    public void deletePpDeviceTest() {
        ppDeviceTestdao.delectPpDeviceTest(getPara("TESTID"), getPara("DEVICEID"));
        renderText("true");
    }
    
    public void updatePpDeviceTest() {
        ppDeviceTestdao.updatePpDeviceTest(getPara("PK"), getPara("TESTID"), getPara("DEVICEID"), getPara("DATA2"), getPara("DATA1"),
                getPara("UNIT"), getPara("MREFLO0"), getPara("MREFHI0"), getPara("FREFLO0"), getPara("FREFHI0"), getPara("Decimalplace"), getPara("SAMPLETYPE"));
        renderText("true");
    }

    public Integer getData1(String DEVICEID) {//获取该仪器最大的打印序号
        List<Record> list = Db.use("mysql").find("select * from pp_device_test WHERE  DEVICEID='" + DEVICEID + "'");
        int value = 1;
        if (list.size() > 0) {
            value = Integer.parseInt(Db.use("mysql").findFirst("SELECT MAX(DATA1) as id from pp_device_test WHERE  DEVICEID='" + DEVICEID + "'").get("id") + "") + 1;
        }
        return value;
    }

    public void updateTestpk() {//修改仪器项目 仪器PK和项目PK
        List<Record> list = Db.use("mysql").find("select pd.PK,pd.TESTID,pd.DEVICEID,t.PK PK2,d.PK PK3    from pp_device_test pd INNER JOIN lis_testdescribe t on pd.TESTID=t.TESTID INNER  JOIN lis_deviceset  d on  pd.DEVICEID=d.DEVICEID");
        for (Record tmp : list) {
            Db.update("update pp_device_test set TESTPK='" + tmp.get("PK2") + "' , DEVICEPK='" + tmp.get("PK3") + "' WHERE TESTID='" + tmp.getStr("TESTID") + "' AND DEVICEID ='" + tmp.getStr("DEVICEID") + "'  ");
            // ppDeviceTestdao.updatTestPK(tmp.getInt("DEVICEID"),tmp.getInt("PK"),tmp.getStr("TESTID"),tmp.getInt("PK2"),tmp.getInt("PK3"));
        }
        renderText("true");        
    }

    public void getRanges() {
        System.out.println(getPara("pk")+"    123123");
        Record re = Db.findFirst("select * from pp_device_test where PK= " + getPara("pk"));
        if (re != null) {
            renderText(re.getStr("DATA3"));
        }else{
            renderText("false");}        
    }

    public void saveRanges() {
        Db.update("update pp_device_test set DATA3 = '" + getPara("range") + "' where PK= " + getPara("pk"));
        renderText("true");
    }
}
